//
//  EMMBaseViewController.m
//  EMMApp
//
//  Created by Mac on 16/4/26.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMBaseViewController.h"
#import "UIViewController+NavBar.h"

@interface EMMBaseViewController ()<UIAlertViewDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong) Class viewClass;

@end

@implementation EMMBaseViewController

#pragma makn - 初始化
- (id)initWithTitle:(NSString *)title {
    return [self initWithTitle:title viewClass:nil];
}

- (id)initWithTitle:(NSString *)title viewClass:(Class)viewClass {
    self = [super init];
    if (self) {
        self.title = title;
        self.viewClass = viewClass;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    /*
    //1.获取系统interactivePopGestureRecognizer对象的target对象
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    //2.创建滑动手势，taregt设置interactivePopGestureRecognizer的target，所以当界面滑动的时候就会自动调用target的action方法。
    //handleNavigationTransition是私有类_UINavigationInteractiveTransition的方法，系统主要在这个方法里面实现动画的。
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
    [pan addTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")];
    //3.设置代理
    pan.delegate = self;
    //4.添加到导航控制器的视图上
    [self.navigationController.view addGestureRecognizer:pan];
    
    //5.禁用系统的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
     */
    [self updateNavBar];
}

//- (BOOL)gestureRecognizerShouldBegin {
//    return YES;
//}

#pragma mark - 滑动开始会触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    //只有导航的根控制器不需要右滑的返回的功能。
    if (self.navigationController.viewControllers.count <= 1) {
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#ifdef __IPHONE_7_0
- (UIRectEdge)edgesForExtendedLayout
{
    return UIRectEdgeNone;
}
#endif

-(void) goBack
{
    NSMutableArray *controllersArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    if(controllersArray.count){
        [controllersArray removeLastObject];
        [self.navigationController popToViewController:controllersArray.lastObject animated:NO];
    }else{
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
}

- (void)configLeftBtn
{
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, 40 ,44)];
    [backBtn addTarget:self action:@selector(backToPreviousPage:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
    [backBtn setTitle:@"   " forState:UIControlStateNormal];
//    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    if (EMM_WIDTH <= 320) {
//        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
//    }else if (EMM_WIDTH > 375){
//        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//    }else{
//        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
//    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    
    
    
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ico_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToPreviousPage:)];
//    [self.navigationItem setLeftBarButtonItem:leftItem];
}

- (void)backToPreviousPage:(id) send {
    [self.hud removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)hideInWindow
{
    AppDelegate* app =(AppDelegate*) [UIApplication sharedApplication].delegate;
    self.hud=[MBProgressUtil showProgressWithMode:MBProgressHUDModeIndeterminate inView:app.window withText:@"请稍候..."];
}

-(void)showHudInWindow:(NSString*) tip
{
    AppDelegate* app =(AppDelegate*) [UIApplication sharedApplication].delegate;
    self.hud=[MBProgressUtil showProgressWithMode:MBProgressHUDModeIndeterminate inView:app.window withText:tip];
}

-(void)showHud
{
    self.hud=[MBProgressUtil showProgressWithMode:MBProgressHUDModeIndeterminate inView:self.navigationController.view?self.navigationController.view:self.view withText:@"请稍候..."];
}

-(void)showHud:(NSString*) tip
{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.hud) {
            [self hideHud];
        }
        self.hud = [MBProgressUtil showProgressWithMode:MBProgressHUDModeIndeterminate inView:self.navigationController.view?self.navigationController.view:self.view withText:tip];
        [self.hud show:YES];
        
       
    });
    
    
}

-(void)showHud:(NSString*) tip inTime:(float)time
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.hud) {
            [self hideHud];
        }
        
        self.hud = [MBProgressUtil showProgressWithMode:MBProgressHUDModeIndeterminate inView:self.view withText:tip inTime:time];
        
                UIGestureRecognizer *tap = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(vg_cancelaction)];
                [self.hud addGestureRecognizer:tap];
    });
    
}

- (void)vg_cancelaction
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.hud) {
            [self hideHud];
        }
    });
}

-(void)showHud:(NSString*)tip model:(BOOL)ismodel{
    if (ismodel) {
        self.hud=[MBProgressUtil showProgressWithMode:MBProgressHUDModeIndeterminate inView:self.navigationController.view?self.navigationController.view:self.view withText:tip];
    }else{
        self.hud=[MBProgressUtil showProgressWithMode:MBProgressHUDModeIndeterminate inView:self.view withText:tip];
    }
}

-(void) pushVC:(UIViewController*) viewController{
    viewController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:viewController animated:NO];
}

-(void)showToast:(NSString *)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressUtil showToast:str inView:self.view];
    });
}

-(void)hideHud{
    dispatch_async(dispatch_get_main_queue(), ^{
        @try {
            if(self.hud){
                [self.hud removeFromSuperview];
                self.hud = nil;
                [self.hud hide:YES];
            }
        }
        @catch (NSException *exception) {
            NSLog(@"hideHud %@",exception.debugDescription);
        }
    });
    
}

- (void)alertWithTitle:(NSString *)title msg:(NSString *)msg {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"确定", nil];
    [alert show];
}

@end
